/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialog_GuestManager.java
 *
 * Created on 2011-5-25, 0:42:48
 */
package lx_easyfinace;

import DB.DatabaseManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;




/**
 *
 * @author echoff <echoffmu@gmail.com>
 */
public class JDialog_GuestManager extends javax.swing.JDialog {

    /** Creates new form JDialog_GuestManager */
    public JDialog_GuestManager(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    public void refalsh(){
        System.out.println("refalsh");
        java.util.Collection data = guestQuery.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        guestList.clear();
        guestList.addAll(data);
        Jtable_GusetManager_list.repaint();
        System.out.println("refalsh end");
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("jdbc:oracle:thin:@localhost:1521:XEPU").createEntityManager();
        guestQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT g FROM Guest g");
        guestList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : guestQuery.getResultList();
        jScrollPane8 = new javax.swing.JScrollPane();
        Jtable_GusetManager_list = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel30 = new javax.swing.JLabel();
        jLabel29 = new javax.swing.JLabel();
        jScrollPane9 = new javax.swing.JScrollPane();
        jEditer_gid = new javax.swing.JEditorPane();
        jScrollPane12 = new javax.swing.JScrollPane();
        jEditer_gphone = new javax.swing.JEditorPane();
        jScrollPane11 = new javax.swing.JScrollPane();
        jEditer_gadd = new javax.swing.JEditorPane();
        jScrollPane10 = new javax.swing.JScrollPane();
        jEditer_gname = new javax.swing.JEditorPane();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        statusPane = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Jtable_GusetManager_list.setSelectionBackground(new java.awt.Color(255, 51, 0));
        Jtable_GusetManager_list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, guestList, Jtable_GusetManager_list);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${gid}"));
        columnBinding.setColumnName("客户编号");
        columnBinding.setColumnClass(java.math.BigDecimal.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${gname}"));
        columnBinding.setColumnName("客户姓名");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${gphone}"));
        columnBinding.setColumnName("客户电话");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${gaddress}"));
        columnBinding.setColumnName("客户地址");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane8.setViewportView(Jtable_GusetManager_list);
        Jtable_GusetManager_list.getColumnModel().getColumn(0).setResizable(false);
        Jtable_GusetManager_list.getColumnModel().getColumn(0).setPreferredWidth(10);
        Jtable_GusetManager_list.getColumnModel().getColumn(1).setResizable(false);
        Jtable_GusetManager_list.getColumnModel().getColumn(2).setResizable(false);
        Jtable_GusetManager_list.getColumnModel().getColumn(3).setResizable(false);
        Jtable_GusetManager_list.getColumnModel().getColumn(3).setPreferredWidth(200);

        getContentPane().add(jScrollPane8, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 540, 310));

        jLabel4.setText("客户编号");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 320, -1, -1));

        jLabel7.setText("客户姓名");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 360, -1, -1));

        jLabel30.setText("客户电话");
        getContentPane().add(jLabel30, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 400, -1, -1));

        jLabel29.setText("客户地址");
        getContentPane().add(jLabel29, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 440, -1, -1));

        jEditer_gid.setEditable(false);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Jtable_GusetManager_list, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.gid}"), jEditer_gid, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jScrollPane9.setViewportView(jEditer_gid);

        getContentPane().add(jScrollPane9, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 320, -1, -1));

        jEditer_gphone.setMinimumSize(new java.awt.Dimension(206, 21));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Jtable_GusetManager_list, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.gphone}"), jEditer_gphone, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jScrollPane12.setViewportView(jEditer_gphone);

        getContentPane().add(jScrollPane12, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 400, -1, -1));

        jEditer_gadd.setMinimumSize(new java.awt.Dimension(206, 21));
        jEditer_gadd.setPreferredSize(new java.awt.Dimension(350, 21));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Jtable_GusetManager_list, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.gaddress}"), jEditer_gadd, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jScrollPane11.setViewportView(jEditer_gadd);

        getContentPane().add(jScrollPane11, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 440, 444, -1));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Jtable_GusetManager_list, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.gname}"), jEditer_gname, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jScrollPane10.setViewportView(jEditer_gname);

        getContentPane().add(jScrollPane10, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 360, -1, -1));

        jButton8.setText("取消");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton8, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 490, 100, 40));

        jButton9.setText("刷新");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton9, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 490, 100, 40));

        jButton10.setText("修改");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton10, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 490, 100, 40));

        jButton7.setText("删除");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 490, 100, 40));

        statusPane.setToolTipText("您现在看到到是客户管理界面");

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);

        javax.swing.GroupLayout statusPaneLayout = new javax.swing.GroupLayout(statusPane);
        statusPane.setLayout(statusPaneLayout);
        statusPaneLayout.setHorizontalGroup(
            statusPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
            .addGroup(statusPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 370, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPaneLayout.setVerticalGroup(
            statusPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPaneLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        getContentPane().add(statusPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 540, 540, -1));

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        try {
            // TODO add your handling code here:
            //        entityManager.getTransaction().commit();
            //        entityManager.getTransaction().begin();
            String gid = jEditer_gid.getText();
            String gname = jEditer_gname.getText();
            String gphone = jEditer_gphone.getText();
            String gadd = jEditer_gadd.getText();
            String sql = "UPDATE guest SET gname='"+gname+"' ,"
                    +"gphone='"+gphone+"' ,"
                    +"GADDRESS='"+gadd+"'  where gid="+gid
                    ;
            PreparedStatement pstm = DatabaseManager.oracleConnect.prepareStatement(sql);
            pstm.execute();
        } catch (SQLException ex) {
            Logger.getLogger(EasyFinace.class.getName()).log(Level.SEVERE, null, ex);
        }
        refalsh();
}//GEN-LAST:event_jButton10ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        int selectedRow = Jtable_GusetManager_list.getSelectedRow();
        String valueAt = Jtable_GusetManager_list.getValueAt(selectedRow, 0).toString();
        System.out.println(valueAt);
        try {
            PreparedStatement pstm = DatabaseManager.oracleConnect.prepareStatement("delete from guest where gid=" + valueAt);
            pstm.execute();
        } catch (SQLException ex) {
            Logger.getLogger(EasyFinace.class.getName()).log(Level.SEVERE, null, ex);
        }
        refalsh();
}//GEN-LAST:event_jButton7ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
        refalsh();
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton8ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                JDialog_GuestManager dialog = new JDialog_GuestManager(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Jtable_GusetManager_list;
    private javax.persistence.EntityManager entityManager;
    private java.util.List<lx_easyfinace.Guest> guestList;
    private javax.persistence.Query guestQuery;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JEditorPane jEditer_gadd;
    private javax.swing.JEditorPane jEditer_gid;
    private javax.swing.JEditorPane jEditer_gname;
    private javax.swing.JEditorPane jEditer_gphone;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane10;
    private javax.swing.JScrollPane jScrollPane11;
    private javax.swing.JScrollPane jScrollPane12;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JScrollPane jScrollPane9;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPane;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
